package com.nowcoder.community.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.jni.Local;

import java.security.acl.Owner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;


public class RedisKeyUtil {
    private static final String SEPARATOR = ":";
    private static final String ENTITY_LIKE_PREFIX = "like:entity";
    private static final String USER_LIKE_PREFIX = "like:user:count";
    private static final String FOLLOWING_PREFIX = "following";
    private static final String FOLLOWER_PREFIX = "follower";
    private static final String KAPTCHA_PREFIX = "kaptcha";
    private static final String LOGIN_TICKET_PREFIX = "loginTicket";
    private static final String USER_CACHE_PREFIX = "cache:user";
    private static final String UV_PREFIX = "uv";
    private static final String DAU_PREFIX = "dau";
    private static final String POST_SCORE_UPDATE_PREFIX = "post:score:update";

    private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");

    // 实体类的赞的redis键 - set(userId...)
    public static String getEntityRedisKey(int entityType, int entityId){
        return StringUtils.joinWith(SEPARATOR, ENTITY_LIKE_PREFIX, entityType, entityId);
    }

    // 用户得到的赞数量统计
    public static String getUserLikeCountRedisKey(int userId){
        return StringUtils.joinWith(SEPARATOR,USER_LIKE_PREFIX, userId);
    }

    // following 关注：用户关注了哪些实体
    public static String getFollowingRedisKey(int userId, int entityType){
        return StringUtils.joinWith(SEPARATOR, FOLLOWING_PREFIX, userId, entityType);
    }

    // follower 粉丝：实体的粉丝
    public static String getFollowerRedisKey(int entityType, int entityId){
        return StringUtils.joinWith(SEPARATOR, FOLLOWER_PREFIX, entityType, entityId);
    }

    // 验证码拥有者
    // owner 用来标识当前准备登录的用户
    public static String getKaptchaOwnerRedisKey(String owner){
        return StringUtils.joinWith(SEPARATOR, KAPTCHA_PREFIX, owner);
    }

    // 用户登录凭证
    public static String getLoginTicketRedisKey(String loginTicket){
        return StringUtils.joinWith(SEPARATOR, LOGIN_TICKET_PREFIX, loginTicket);
    }

    // 用户信息缓存
    public static String getUserCacheRedisKey(int userId){
        return StringUtils.joinWith(SEPARATOR,USER_CACHE_PREFIX, userId);
    }

    /**
     * 单日 UV redies key
     * */
    public static String getUVRedisKey(LocalDate date){
        return StringUtils.joinWith(SEPARATOR,UV_PREFIX, date.format(dateFormat));
    }

    /**
     * 区间 UV redis key
     * */
    public static String getUVRedisKey(LocalDate start, LocalDate end){
        return StringUtils.joinWith(SEPARATOR, UV_PREFIX, start.format(dateFormat), end.format(dateFormat));
    }

    /**
     * 单日 DAU
     * */
    public static String getDAURedisKey(LocalDate date){
        return StringUtils.joinWith(SEPARATOR, DAU_PREFIX, date.format(dateFormat));
    }

    /**
     * 区间 DAU
     * */
    public static String getDAURedisKey(LocalDate start, LocalDate end){
        return StringUtils.joinWith(SEPARATOR,DAU_PREFIX, start.format(dateFormat), end.format(dateFormat));
    }

    public static String getPostScoreUpdateRedisKey(){
        return POST_SCORE_UPDATE_PREFIX;
    }
}
